// 优化版导航栏管理脚本
class NavbarManager {
    constructor() {
        this.currentPage = this.getCurrentPage();
        this.init();
    }

    init() {
        this.createNavbar();
        this.setupEventListeners();
        this.setActiveNavItem();
        this.setupScrollEffects();
    }

    // 获取当前页面信息
    getCurrentPage() {
        const path = window.location.pathname;
        const pageMap = {
            'dashboard.html': { name: '仪表盘', icon: 'fas fa-tachometer-alt' },
            'reports.html': { name: '访客统计', icon: 'fas fa-chart-line' },
            'blog.html': { name: '博客管理', icon: 'fas fa-blog' },
            'gallery.html': { name: '图库管理', icon: 'fas fa-images' },

            'logs.html': { name: '网站日志', icon: 'fas fa-file-alt' },
            'system-settings.html': { name: '系统设置', icon: 'fas fa-cog' },
            'messages.html': { name: '消息中心', icon: 'fas fa-comments' },
            'category-manager.html': { name: '分类管理', icon: 'fas fa-tags' }
        };

        const fileName = path.split('/').pop();
        return pageMap[fileName] || { name: '后台管理', icon: 'fas fa-cog' };
    }

    // 创建导航栏HTML结构
    createNavbar() {
        const navbarHTML = `
            <nav class="navbar">
                <div class="navbar-container">
                    <!-- 左侧：品牌 -->
                    <div class="navbar-left">
                        <a href="dashboard.html" class="navbar-brand">
                            <span>后台管理系统</span>
                        </a>
                    </div>

                    <!-- 中间：主导航菜单 -->
                    <div class="navbar-center">
                        <ul class="navbar-nav">
                            <li class="nav-item">
                                <a href="dashboard.html" class="nav-link" data-page="dashboard">
                                    <span>仪表盘</span>
                                </a>
                            </li>
                            <li class="nav-item">
                                <a href="reports.html" class="nav-link" data-page="reports">
                                    <span>访客统计</span>
                                </a>
                            </li>
                            <li class="nav-item">
                                <a href="blog.html" class="nav-link" data-page="blog">
                                    <span>博客管理</span>
                                </a>
                            </li>
                            <li class="nav-item">
                                <a href="gallery.html" class="nav-link" data-page="gallery">
                                    <span>图库管理</span>
                                </a>
                            </li>


                            <li class="nav-item">
                                <a href="messages.html" class="nav-link" data-page="messages">
                                    <span>留言订阅</span>
                                </a>
                            </li>
                            <li class="nav-item">
                                <a href="category-manager.html" class="nav-link" data-page="category-manager">
                                    <span>分类管理</span>
                                </a>
                            </li>

                        </ul>
                    </div>

                    <!-- 右侧：管理员信息下拉菜单 -->
                    <div class="navbar-right">
                        <div class="admin-dropdown">
                            <div class="admin-info" onclick="navbarManager.toggleDropdown()">
                                <div class="admin-avatar">
                                    <img src="" alt="管理员头像">
                                </div>
                                <div class="admin-details">
                                    <span class="admin-name">管理员</span>
                                    <span class="admin-role">系统管理员</span>
                                </div>
                                <i class="fas fa-chevron-down dropdown-arrow"></i>
                            </div>
                            <div class="dropdown-menu">
                                <a href="profile-main.html" class="dropdown-item">
                                    <i class="fas fa-user"></i>
                                    <span>个人资料</span>
                                </a>
                            </div>
                        </div>
                    </div>
                </div>
            </nav>
        `;

        // 插入导航栏到页面顶部
        document.body.insertAdjacentHTML('afterbegin', navbarHTML);
        document.body.classList.add('has-navbar');
    }

    // 设置事件监听器
    setupEventListeners() {
        // 滚动效果
        window.addEventListener('scroll', this.handleScroll.bind(this));
        
        // 导航链接点击
        document.addEventListener('click', this.handleNavClick.bind(this));
        
        // 窗口大小变化
        window.addEventListener('resize', this.handleResize.bind(this));
        
        // 点击外部关闭下拉菜单
        document.addEventListener('click', this.handleOutsideClick.bind(this));
    }

    // 设置滚动效果
    setupScrollEffects() {
        this.handleScroll();
    }

    // 处理滚动
    handleScroll() {
        const navbar = document.querySelector('.navbar');
        if (navbar) {
            if (window.scrollY > 50) {
                navbar.classList.add('scrolled');
            } else {
                navbar.classList.remove('scrolled');
            }
        }
    }

    // 设置移动端导航 - 简化版本
    setupMobileNavigation() {
        // 移动端导航功能已移除
    }

    // 处理导航链接点击
    handleNavClick(event) {
        const navLink = event.target.closest('.nav-link');
        if (navLink && !navLink.classList.contains('active')) {
            // 移除所有激活状态
            document.querySelectorAll('.nav-link').forEach(link => {
                link.classList.remove('active');
            });
            
            // 添加当前激活状态
            navLink.classList.add('active');
        }
    }

    // 设置激活的导航项
    setActiveNavItem() {
        const currentPage = this.getCurrentPageName();
        const activeLink = document.querySelector(`[data-page="${currentPage}"]`);
        
        if (activeLink) {
            activeLink.classList.add('active');
        }
    }

    // 获取当前页面名称
    getCurrentPageName() {
        const path = window.location.pathname;
        const pageMap = {
            'dashboard.html': 'dashboard',
            'reports.html': 'reports',
            'blog.html': 'blog',
            'gallery.html': 'gallery',
            'orders.html': 'orders',
            'messages.html': 'messages',
            'logs.html': 'logs',
            'system-settings.html': 'settings',
            'category-manager.html': 'categories'
        };

        const fileName = path.split('/').pop();
        return pageMap[fileName] || 'dashboard';
    }

    // 处理窗口大小变化
    handleResize() {
        // 简化处理
    }

    // 切换下拉菜单
    toggleDropdown() {
        const dropdown = document.querySelector('.admin-dropdown');
        if (dropdown) {
            dropdown.classList.toggle('active');
        }
    }

    // 关闭下拉菜单
    closeDropdown() {
        const dropdown = document.querySelector('.admin-dropdown');
        if (dropdown) {
            dropdown.classList.remove('active');
        }
    }

    // 处理外部点击
    handleOutsideClick(event) {
        const dropdown = document.querySelector('.admin-dropdown');
        const adminInfo = document.querySelector('.admin-info');
        
        if (dropdown && adminInfo && !adminInfo.contains(event.target)) {
            this.closeDropdown();
        }
    }

    // 退出登录
    logout() {
        if (confirm('确定要退出登录吗？')) {
            // 这里可以添加退出登录的逻辑
            window.location.href = 'login.html';
        }
    }
}

// 创建全局导航栏管理器实例
let navbarManager;

// 页面加载完成后初始化
document.addEventListener('DOMContentLoaded', function() {
    // 检查当前页面是否为博客编辑器，如果是则跳过导航栏初始化
    const currentPage = window.location.pathname.split('/').pop();
    if (currentPage !== 'editor.html') {
        navbarManager = new NavbarManager();
        
        // 定期更新徽章计数（模拟实时数据）
        setInterval(() => {
            if (navbarManager && navbarManager.updateBadgeCounts) {
                navbarManager.updateBadgeCounts();
            }
        }, 30000); // 每30秒更新一次
    }
});

// 导出到全局作用域
window.navbarManager = navbarManager;